<%@page import="descriptor.EntityPresentation"%>
<%@page import="html.InputType"%>
<%@page import="html.XHtmlFormatter"%>
<%@page import="general.OutputProvider"%>
<%@page import="descriptor.Property"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Collection"%>
<%@page import="web.ParamStorage"%>
<%@page import="formatters.InputFormatter"%>
<%@page import="formatters.EditFormatter"%>
<%@page import="formatters.ViewFormatter"%>
<%@page import="formatters.Formatter"%>
<%@page import="presentation.PresentationElementFactory"%>
<%@page import="presentation.PresentationElement"%>
<%@page import="java.util.Date"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<%@page import="javax.sql.DataSource"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="database.DatabaseConnection"%>
<%@page import="database.DatabaseText"%>
<%@page import="runtime.RuntimeInstanceFactory"%>
<%@page import="database.DatabaseObjectFactory"%>
<%@page import="database.DatabaseNumber"%>
<%@page import="database.DatabaseObject"%>
<%@page import="exceptions.DatabaseException"%>
<%@page import="java.io.File"%>
<%@page import="java.util.List"%>
<%@page import="descriptor.EntityDAO"%>
<%@page import="descriptor.Application"%>
<%@page import="descriptor.Template"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<%!
    Application app = Application.getInstance();
    String currentTable = "";
    XHtmlFormatter provider;
%>

<%
    provider = new XHtmlFormatter(out);
    provider.startHtml();
    provider.startHead();
    provider.writeMeta("Content-Language", "en-US");
    provider.writeMeta("Content-Type", "text/xhtml+xml; charset=utf-8");
    provider.writeLink("stylesheet", "text/css", "styles.css");
    provider.writeTitle(app.getTitle());
    provider.endHead();
    provider.endHead();
    provider.startBody();
%>
<%
    provider.startDiv("header");
    provider.startDiv();
    provider.writeH1(app.getTitle());
    provider.endDiv();
    provider.endDiv();
%>

<%
    provider.startForm("index.jsp", "post");
    provider.startDiv("control");
    ParamStorage storage = new ParamStorage(request);
    if (storage.hasParam("table-selection")) {
        currentTable = storage.getParam("table-selection");
    }
    Iterable<Template> templateCollection = app.getTemplateCollection();
    for (Template t: templateCollection) {
        if (storage.hasParam("table-" + t.getAlias())) {
            currentTable = t.getAlias();
            break;
        }
    }
    provider.writeInput(InputType.hidden, "table-selection", "", "", currentTable);
    provider.startDiv("", "selectPanel");
    boolean first = true;
    Iterator<Template> it = templateCollection.iterator();
    while (it.hasNext()) {
        Template t = it.next();
        String styleClass = "tab";
        if (first) { 
            styleClass = "left-tab";
            first = false;
        }
        if (!it.hasNext())
            styleClass = "right-tab";
        provider.writeInput(InputType.submit, "table-" + t.getAlias(), "", styleClass, t.getName());
    }
    provider.endDiv();
    provider.startDiv("", "optionsPanel");
    provider.echo("TODO: add control buttons here ");
    provider.endDiv();
    provider.endDiv();
%>

<%
    provider.startDiv("content");
    if (!currentTable.isEmpty()) {
        try {
            provider.startDiv("", "tablePanel");
            EntityDAO dao = new EntityDAO();
            dao.bind(currentTable);
            provider.startDiv();
            provider.writeH2(app.getTemplate(currentTable).getTableName());
            provider.endDiv();
            Iterable<EntityPresentation> container = dao.findAll();
            provider.startTable();
            Formatter formatter = new ViewFormatter();
            for(EntityPresentation en: container) {
                provider.startTr();
                Iterable<PresentationElement> elements = en.getPresentationCollection();
                for(PresentationElement p: elements) {
                    provider.startTd();
                    out.print(formatter.format(p));
                    provider.endTd();
                }
                provider.endTr();
            }
            provider.endTable();
            provider.endDiv();
        } catch (RuntimeException ex) {
            out.write(ex.getMessage());
        } catch (DatabaseException ex) {
            out.write(ex.getMessage());
        }
    } else {

    }
    provider.endDiv();
    provider.endForm();
%>

<%
    provider.startDiv("footer");
    provider.writeP("center", "Copyright Alexander Pyatkin, 2011");
    provider.endDiv();
    provider.endBody();
    provider.endHtml();
%>

